package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.ContactsBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/9/29 0029.
 */

public class CreateGroupSearchFriendResultAdapter extends BaseRecyclerViewAdapter<ContactsBean> {

    private Context mContext;
    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public CreateGroupSearchFriendResultAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_create_group_friends_search_result, parent, false);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final ContactsBean bean = mList.get(position);
            ((ViewHolder) holder).mCbIsChoice.setChecked(bean.isChecked());
            ((ViewHolder) holder).mTvFriendsName.setText(bean.getName());
            ((ViewHolder) holder).mTvFriendsSchool.setText(bean.getUnit());
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .override(350, 350);
            Glide.with(mContext.getApplicationContext())
                    .load(TextUtils.isEmpty(
                            bean.getAvatar()) ?
                            R.mipmap.default_avatar :
                            HttpRequestConstant.USER_PIC_HEAD + bean.getAvatar())
                    .apply(requestOptions)
                    .into(((ViewHolder) holder).mCiFriendsHeader);
            ((ViewHolder) holder).mCbIsChoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemClick(bean);
                    }
                }
            });
            ((ViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemClick(bean);
                    }
                }
            });
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cb_is_choice)
        CheckBox mCbIsChoice;
        @BindView(R.id.ci_friends_header)
        CircleImageView mCiFriendsHeader;
        @BindView(R.id.tv_friends_name)
        TextView mTvFriendsName;
        @BindView(R.id.tv_friends_school)
        TextView mTvFriendsSchool;

        private View itemView;

        public ViewHolder(View view) {
            super(view);
            itemView = view;
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(ContactsBean bean);
    }
}
